package com.leyou.gateway.config;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

public class RateLimitConfig {
    //给不同的请求IP地址设置不同令牌桶
    @Bean
    public KeyResolver ipKeyResolver() {
        return new KeyResolver() {
            @Override
            /**
             * Mono<String>:给令牌桶生成的key
             */
            public Mono<String> resolve(ServerWebExchange exchange) {
                return Mono.just(exchange.getRequest().getRemoteAddress().getHostName());
            }
        };
    }
}
